if(condition) { statements ---------- } else if(condition) { statements ---------- } else if(condition) { statements ---------- } else ---------
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.print("Enter a Number:"); int a=stdin.nextInt(); if(a>0) System.out.println("Number is Positive.."); else if(a⁢0) System.out.println("Number is Negative.."); else System.out.println("Number is Zero.."); } }
Enter a Number 56 Number is positive
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter 3 different Numbers:"); int a=stdin.nextInt(); int b=stdin.nextInt(); int c=stdin.nextInt(); if(a>b && a>c) System.out.println(a+" is Greatest"); else if(b>c) System.out.println(b+" is Greatest"); else System.out.println(c+" is Greatest"); } }
Enter 3 different Numbers 31 7 78 Greatest number is 78
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.print("Enter Color Code:"); String str=stdin.next(); char ch=str.charAt(0); //charAt method is used to get char from pos if(ch=='r' || ch=='R') System.out.println("Red"); else if(ch=='g' || ch=='G') System.out.println("Green"); else if(ch=='b' || ch=='B') System.out.println("Blue"); else System.out.println("White"); } }
Enter Color Code B Blue